import component from 'element-plus/es/components/tree-select/src/tree-select-option.mjs'
import { createRouter,createWebHashHistory,createWebHistory } from 'vue-router'
import Index from '@/pages/index.vue'
import Login from '@/pages/login/login2.vue'
import Register from '@/pages/login/register.vue'
import NotFound from '@/pages/404.vue'
import Admin from "@/layouts/admin.vue";
import GoodList from "@/pages/goods/list.vue";
import CategoryList from "@/pages/category/list.vue";
import UserList from '@/pages/user/list.vue'
import UserAuth from '@/pages/user/UserAuth.vue'
import AuthList from '@/pages/user/AuthList.vue'
import OrderList from '@/pages/order/list.vue'
import CommentList from '@/pages/comment/list.vue'
import ImageList from '@/pages/image/list.vue'
import NoticeList from '@/pages/notice/list.vue'
import NoticeDetail from '@/pages/notice/detail.vue'
import SettingBase from '@/pages/setting/base.vue'
import CouponList from '@/pages/coupon/list.vue'
import Manager from '@/pages/manager/list.vue'
import Access from '@/pages/access/list.vue'
import GoodDetail from "@/pages/goods/detail.vue";
import DrawModel from '@/pages/moban/DrawModel.vue'
import DialogModel from '@/pages/moban/DialogModel.vue'
import BeforePageModel from '@/pages/moban/BeforePage.vue'
// 默认路由，所有用户共享
const routes = [
    {
        path: "/",
        name:"admin",
        component: Admin,
        // children:[
        //     {
        //         path:"/notice/detail/:id",
        //         name:"/notice/detail",
        //         component:NoticeDetail,
        //         meta:{title:"notice列表"},              
          
        // },
        // ]
    },
    {
        path: "/login",
        component: Login,
        meta: {
            title: "登录页"
        }
    }, 
    {
        path: "/register",
        component: Register,
        meta: {
            title: "注册页"
        }
    }, 
    

    {
        path: '/:pathMatch(.*)*',
        name: 'NotFound',
        component: NotFound
    }]


// 动态路由，用于匹配菜单动态添加路由
const asyncRoutes = [{
    path:"/",
    name:"/",
    component:Index,
    meta:{
        title:"后台首页"
    }
},
{
    path:"/draw/list",
    name:"/draw/list",
    component:DrawModel,
    meta:{
        title:"抽屉模版"
    }
},
{
    path:"/dialog/list",
    name:"/dialog/list",
    component:DialogModel,
    meta:{
        title:"dialog模版"
    }
},
{
    path:"/beforepage/list",
    name:"/beforepage/list",
    component:BeforePageModel,
    meta:{
        title:"前端分页模版"
    }
},
{
    path:"/manager/list",
    name:"/manager/list",
    component:Manager,
    meta:{
        title:"管理员管理"
    }
},
{
    path:"/goods/list",
    name:"/goods/list",
    component:GoodList,
    meta:{
        title:"商品管理"
    }
},
{
    path:"/goods/detail/:id",
    name:"/goods/detail",
    component:GoodDetail,
    hidden: true ,  //是否显示在菜单
    meta:{
        title:"商品详情页"
    }
},
{
    path:"/category/list",
    name:"/category/list",
    component:CategoryList,
    meta:{
        title:"分类列表"
    }
},{
    path:"/user/list",
    name:"/user/list",
    component:UserList,
    meta:{
        title:"用户列表"
    }
},{
    path:"/order/list",
    name:"/order/list",
    component:OrderList,
    meta:{
        title:"订单列表"
    }
},{
    path:"/comment/list",
    name:"/comment/list",
    component:CommentList,
    meta:{
        title:"评价列表"
    }
},{
    path:"/image/list",
    name:"/image/list",
    component:ImageList,
    meta:{
        title:"图库列表"
    }
},{
    path:"/notice/list",
    name:"/notice/list",
    component:NoticeList,
    meta:{
        title:"公告列表"
    },
},{
    path:"/access/list",
    name:"/access/list",
    component:Access,
    meta:{
        title:"菜单管理"
    }
},
{
    path:"/user/auth",
    name:"/user/auth",
    component:UserAuth,
    meta:{
        title:"用户权限"
    }
},
{
    path:"/user/authDetail",
    name:"/user/authDetail",
    component:AuthList,
    meta:{
        title:"权限列表"
    }
},
{
    path:"/coupon/list",
    name:"/coupon/list",
    component:CouponList,
    meta:{
        title:"优惠券列表"
    }
}]

export const router = createRouter({
    history:createWebHistory(import.meta.env.BASE_URL),
    routes

})
// 动态递归添加路由的方法
export function addRoutes(menus){
    // 是否有新的路由
    let hasNewRoutes = false
    const findAndAddRoutesByMenus = (arr) =>{
        arr.forEach(e=>{
            // console.log('e',e)
            let item = asyncRoutes.find(o=>o.path == e.frontpath)
            // console.log('item',item)
            // let item = e
            if(item && !router.hasRoute(item.path)){
                router.addRoute("admin",item)
                hasNewRoutes = true
            }
            if(e.child && e.child.length > 0){               
                findAndAddRoutesByMenus(e.child)
            }
        })
    }

    findAndAddRoutesByMenus(menus)

    return hasNewRoutes
}


